package com.sansan.qiangji.controller.device;

import com.sansan.qiangji.VO.device.StackerVO;
import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import com.sansan.qiangji.form.device.StackerAddForm;
import com.sansan.qiangji.form.device.StackerEditForm;
import com.sansan.qiangji.realize.device.StackerReal;
import com.sansan.qiangji.resultVO.ResultVO;
import com.sansan.qiangji.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

@CrossOrigin()
@RestController
@RequestMapping("/stacker")
@Slf4j
public class StackerController {
    private StackerReal stackerReal;

    @Resource
    public void setStackerReal(StackerReal stackerReal){
        this.stackerReal = stackerReal;
    }

    @GetMapping("/list")
    public ResultVO<Object> stackerList() {
        List<StackerVO> stackerVOS = stackerReal.stackerList();
        return ResultVOUtil.success(stackerVOS);
    }

    @PostMapping("/create")
    public ResultVO<Object> stackerCreate(@Valid @RequestBody StackerAddForm stackerAddForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【添加堆垛机】参数不正确，stackerAddForm={}", stackerAddForm.toString());
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        stackerReal.stackerCreate(stackerAddForm);
        return ResultVOUtil.success(null);
    }

    @PostMapping("/edit")
    public ResultVO<Object> stackerEdit(@Valid @RequestBody StackerEditForm stackerEditForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【编辑堆垛机】参数不正确，stackerEditForm={}", stackerEditForm.toString());
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        stackerReal.stackerEdit(stackerEditForm);
        return ResultVOUtil.success(null);
    }

    @PostMapping("/delete")
    public ResultVO<Object> deleteStacker(@RequestParam(name = "stackerId") Integer stackerId) {
        stackerReal.deleteStacker(stackerId);
        return ResultVOUtil.success(null);
    }

    @PostMapping("/getStackerParamDetail")
    public ResultVO<Object> getStackerParamDetail(@RequestParam(name = "stackerId") Integer stackerId) {
        return ResultVOUtil.success(stackerReal.getStackerParamDetail(stackerId));
    }
}
